package demo.interceptor;

import demo.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.logging.Handler;
@Component
public class UserInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        System.out.println(uri);
        if(uri.contains("/login")){return true;}
        HttpSession session = request.getSession();
        if(session.getAttribute("user")!=null)
        {
            return true;
        }
        request.setAttribute("msg","您还没有登录请登录");
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,response);
        return false;
    }
}
